
package com.iplant.basic.controller.mss;

import com.iplant.basic.service.MSSService;
import com.iplant.basic.service.po.mss.MSSUnit;
import com.iplant.basic.service.po.mss.MSSUnitGroup;
import com.iplant.base.controller.BaseController;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.ServiceResult;
import com.iplant.base.utils.RetCode;
import com.iplant.base.utils.serializer.CloneTool;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.basic.service.po.bms.BMSEmployee;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/Unit")
public class UnitController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(UnitController.class);

    @Autowired
    MSSService wMSSService;

    @GetMapping("/All")
    public Object All(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            int wGroupID = StringUtils.parseInt(request.getParameter("GroupID"), -1);
            String wGroupFullName = StringUtils.parseString(request.getParameter("GroupFullName"));
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"), -1);
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<MSSUnit>> wServiceResult = wMSSService.MSS_QueryUnitList(wLoginUser, wGroupID,
                    wGroupFullName, wName, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            ServiceResult<MSSUnit> wServiceResult = wMSSService.MSS_QueryUnit(wLoginUser, wID, wCode);
            MSSUnit wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Update")
    public Object Update(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            MSSUnit wMSSUnit = CloneTool.Clone(wParam.get("data"), MSSUnit.class);
            wMSSUnit.EditorName = wUserName;
            wMSSUnit.EditorID = wUserID;
            wMSSUnit.EditTime = Calendar.getInstance();
            if (wMSSUnit.ID <= 0) {

                wMSSUnit.CreatorName = wUserName;
                wMSSUnit.CreatorID = wUserID;
                wMSSUnit.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wMSSService.MSS_UpdateUnit(wBMSEmployee, wMSSUnit);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSUnit);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Delete")
    public Object Delete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            MSSUnit wMSSUnit = CloneTool.Clone(wParam.get("data"), MSSUnit.class);

            ServiceResult<Integer> wServiceResult = wMSSService.MSS_DeleteUnit(wBMSEmployee, wMSSUnit);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/Active")
    public Object Active(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<MSSUnit> wMSSUnitList = CloneTool.CloneArray(wParam.get("data"), MSSUnit.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (MSSUnit wMSSUnit : wMSSUnitList) {

                wServiceResult = wMSSService.MSS_ActiveUnit(wBMSEmployee, wMSSUnit, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/GroupAll")
    Object GroupAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);
            int wParentGroupID = StringUtils.parseInt(request.getParameter("ParentGroupID"));
            String wGroupFullName = StringUtils.parseString(request.getParameter("GroupFullName"));
            String wName = StringUtils.parseString(request.getParameter("Name"));
            int wActive = StringUtils.parseInt(request.getParameter("Active"));
            Pagination wPagination = GetPagination(request);
            ServiceResult<List<MSSUnitGroup>> wServiceResult = wMSSService.MSS_QueryUnitGroupList(wLoginUser,
                    wParentGroupID, wGroupFullName, wName, wActive, wPagination);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, wPagination.getTotalPage());
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @GetMapping("/GroupInfo")
    Object GroupInfo(HttpServletRequest request) {
        Object wResult = new Object();
        try {

            BMSEmployee wLoginUser = GetSession(request);

            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));

            ServiceResult<MSSUnitGroup> wServiceResult = wMSSService.MSS_QueryUnitGroup(wLoginUser, wID, wCode);
            MSSUnitGroup wServerRst = wServiceResult.Result;

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServerRst);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode(), null, wServerRst);
            }

        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/GroupUpdate")
    Object GroupUpdate(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);
            int wUserID = wBMSEmployee.ID;
            String wUserName = wBMSEmployee.Name;

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            MSSUnitGroup wMSSUnitGroup = CloneTool.Clone(wParam.get("data"), MSSUnitGroup.class);
            wMSSUnitGroup.EditorName = wUserName;
            wMSSUnitGroup.EditorID = wUserID;
            wMSSUnitGroup.EditTime = Calendar.getInstance();
            if (wMSSUnitGroup.ID <= 0) {

                wMSSUnitGroup.CreatorName = wUserName;
                wMSSUnitGroup.CreatorID = wUserID;
                wMSSUnitGroup.CreateTime = Calendar.getInstance();

            }
            ServiceResult<Integer> wServiceResult = wMSSService.MSS_UpdateUnitGroup(wBMSEmployee, wMSSUnitGroup);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wMSSUnitGroup);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/GroupDelete")
    Object GroupDelete(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            MSSUnitGroup wMSSUnitGroup = CloneTool.Clone(wParam.get("data"), MSSUnitGroup.class);

            ServiceResult<Integer> wServiceResult = wMSSService.MSS_DeleteUnitGroup(wBMSEmployee, wMSSUnitGroup);

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }

    @PostMapping("/GroupActive")
    Object GroupActive(HttpServletRequest request, @RequestBody Map<String, Object> wParam) {
        Object wResult = new Object();
        try {

            BMSEmployee wBMSEmployee = GetSession(request);

            if (!wParam.containsKey("data")) {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, RetCode.SERVER_RST_ERROR_OUT);
                return wResult;
            }

            int wActive = wParam.containsKey("Active") ? (int) wParam.get("Active") : 0;

            List<MSSUnitGroup> wMSSUnitGroupList = CloneTool.CloneArray(wParam.get("data"), MSSUnitGroup.class);
            ServiceResult<Integer> wServiceResult = new ServiceResult<Integer>(0);
            for (MSSUnitGroup wMSSUnitGroup : wMSSUnitGroupList) {

                wServiceResult = wMSSService.MSS_ActiveUnitGroup(wBMSEmployee, wMSSUnitGroup, wActive);

                if (!StringUtils.isEmpty(wServiceResult.getFaultCode()))
                    break;
            }

            if (StringUtils.isEmpty(wServiceResult.getFaultCode())) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "");
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.getFaultCode());
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString());
        }
        return wResult;
    }


}
